<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    
    <title>Python常用方法及函数 | SongKaBaKa</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="经验">
    <meta name="description" content="Python常用的方法及函数，建议按数据类型进行区分，主要集中于字符串处理，再另外，务必关注返回值类型、是否对源数据进行修改以及一些特殊规定 查看数据类型 type() 转换为各种进制后输出： 二进制 bin() 八进制 oct() 十六进制 hex()  强制类型转换： 转换为字符串 str() 转换为整型 int() 转换为浮点型 float() 转换为布尔型 bool() int(vlaue">
<meta property="og:type" content="article">
<meta property="og:title" content="Python常用方法及函数">
<meta property="og:url" content="https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="SongKaBaKa">
<meta property="og:description" content="Python常用的方法及函数，建议按数据类型进行区分，主要集中于字符串处理，再另外，务必关注返回值类型、是否对源数据进行修改以及一些特殊规定 查看数据类型 type() 转换为各种进制后输出： 二进制 bin() 八进制 oct() 十六进制 hex()  强制类型转换： 转换为字符串 str() 转换为整型 int() 转换为浮点型 float() 转换为布尔型 bool() int(vlaue">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-07-17T07:20:36.000Z">
<meta property="article:modified_time" content="2020-07-17T11:04:58.986Z">
<meta property="article:author" content="Song Wani">
<meta property="article:tag" content="经验">
<meta name="twitter:card" content="summary">
    
    <link rel="shortcut icon" href="/blog/favicon.ico">
    <link rel="stylesheet" href="//unpkg.com/hexo-theme-material-indigo@latest/css/style.css">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

<meta name="generator" content="Hexo 4.2.1"></head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/blog/img/brand.jpg)">
      <div class="brand">
        <a href="/blog/" class="avatar waves-effect waves-circle waves-light">
          <img src="/blog/img/avatar.jpg">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">Song Wani</h5>
          <a href="mailto:bugsong@wo.cn" title="bugsong@wo.cn" class="mail">bugsong@wo.cn</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/blog/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/blog/categories"  >
                <i class="icon icon-lg icon-th-list"></i>
                Categories
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://songbug.gitee.io/blog" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">Python常用方法及函数</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="Search">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">Python常用方法及函数</h1>
        <h5 class="subtitle">
            
                <time datetime="2020-07-17T07:20:36.000Z" itemprop="datePublished" class="page-time">
  2020-07-17
</time>


	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%BC%96%E7%A8%8B/">编程</a></li></ul>

            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
    <aside class="post-widget">
        <nav class="post-toc-wrap post-toc-shrink" id="post-toc">
            <h4>TOC</h4>
            <ol class="post-toc"><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#转换为各种进制后输出："><span class="post-toc-number">1.</span> <span class="post-toc-text">转换为各种进制后输出：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#强制类型转换："><span class="post-toc-number">2.</span> <span class="post-toc-text">强制类型转换：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#随机数模块："><span class="post-toc-number">3.</span> <span class="post-toc-text">随机数模块：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#切片的语法："><span class="post-toc-number">4.</span> <span class="post-toc-text">切片的语法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#查找内容相关方法："><span class="post-toc-number">5.</span> <span class="post-toc-text">查找内容相关方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#判断相关方法："><span class="post-toc-number">6.</span> <span class="post-toc-text">判断相关方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#replace-方法："><span class="post-toc-number">7.</span> <span class="post-toc-text">replace() 方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#字符串分隔方法："><span class="post-toc-number">8.</span> <span class="post-toc-text">字符串分隔方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#控制大小写的方法："><span class="post-toc-number">9.</span> <span class="post-toc-text">控制大小写的方法：</span></a></li><li class="post-toc-item post-toc-level-4"><a class="post-toc-link" href="#编码与解码："><span class="post-toc-number">10.</span> <span class="post-toc-text">编码与解码：</span></a></li></ol>
        </nav>
    </aside>


<article id="post-Python常用的方法及函数"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">Python常用方法及函数</h1>
        <div class="post-meta">
            <time class="post-time" title="2020-07-17 15:20:36" datetime="2020-07-17T07:20:36.000Z"  itemprop="datePublished">2020-07-17</time>

            
	<ul class="article-category-list"><li class="article-category-list-item"><a class="article-category-list-link" href="/blog/categories/%E7%BC%96%E7%A8%8B/">编程</a></li></ul>



            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>Python常用的方法及函数，建议按数据类型进行区分，主要集中于字符串处理，再另外，务必关注返回值类型、是否对源数据进行修改以及一些特殊规定</p>
<p>查看数据类型 type()</p>
<h4 id="转换为各种进制后输出："><a href="#转换为各种进制后输出：" class="headerlink" title="转换为各种进制后输出："></a>转换为各种进制后输出：</h4><ul>
<li>二进制 bin()</li>
<li>八进制 oct()</li>
<li>十六进制 hex()</li>
</ul>
<h4 id="强制类型转换："><a href="#强制类型转换：" class="headerlink" title="强制类型转换："></a>强制类型转换：</h4><ul>
<li>转换为字符串 str()</li>
<li>转换为整型 int()</li>
<li>转换为浮点型 float()</li>
<li>转换为布尔型 bool()</li>
<li>int(vlaue,进制)，表示将某进制数转换为十进制数，其他以此类推</li>
</ul>
<h4 id="随机数模块："><a href="#随机数模块：" class="headerlink" title="随机数模块："></a>随机数模块：</h4><p>random() 可以使用 random.randint(m,n) 生成由m到n的随机整数</p>
<ul>
<li>pass的作用：</li>
</ul>
<p>保证语句完整性</p>
<h4 id="切片的语法："><a href="#切片的语法：" class="headerlink" title="切片的语法："></a>切片的语法：</h4><ul>
<li>valuename[​start：end：step​]</li>
</ul>
<h4 id="查找内容相关方法："><a href="#查找内容相关方法：" class="headerlink" title="查找内容相关方法："></a>查找内容相关方法：</h4><ul>
<li>find() 获得索引，不存在返回 -1</li>
<li>index() 获得索引，不存在报错</li>
<li>还有带r的，即从右往左的</li>
</ul>
<h4 id="判断相关方法："><a href="#判断相关方法：" class="headerlink" title="判断相关方法："></a>判断相关方法：</h4><ul>
<li>返回值是bool值</li>
<li>is开头<ul>
<li>isdigit()</li>
<li>isalpha() 是否单纯包含数字，字符</li>
</ul>
</li>
<li>startswith() 以什么开头，可以是字符串</li>
<li>endswith()</li>
</ul>
<h4 id="replace-方法："><a href="#replace-方法：" class="headerlink" title="replace() 方法："></a>replace() 方法：</h4><p>将字符串中出现的某个字符用指定字符替换，亦强调字符串是<em>不可变数据类型</em></p>
<h4 id="字符串分隔方法："><a href="#字符串分隔方法：" class="headerlink" title="字符串分隔方法："></a>字符串分隔方法：</h4><ul>
<li>split() 将字符串用指定字符分割为列表，关注参数和返回值</li>
<li>splitlines() 换行符分隔</li>
<li>partition() 指定一个字符串作为分隔符，返回一个元组，分前段，本身，后段</li>
</ul>
<h4 id="控制大小写的方法："><a href="#控制大小写的方法：" class="headerlink" title="控制大小写的方法："></a>控制大小写的方法：</h4><ul>
<li>capitalize() 仅大写第一个单词首字母，用于处理英语句子</li>
<li>title() 每个独立单词首字母大写，可以认出_</li>
<li>upper() 全大写</li>
<li>lower() 全小写</li>
<li>ljust() 让字符串以指定的长度显示，少则补全空格，多则不作操作，根据参数自行研究</li>
<li>rjust() </li>
<li>center() 单词居中，两边加字符补全</li>
<li>strip() 清楚指定字符，默认空白符</li>
<li>lstrip()</li>
<li>rstrip()</li>
<li>join() 指定符号分隔元素，返回值为新的字符串，注意字符串不能被修改，但是字符串变量可以被重新赋值</li>
</ul>
<h4 id="编码与解码："><a href="#编码与解码：" class="headerlink" title="编码与解码："></a>编码与解码：</h4><ul>
<li><p>函数：</p>
<ul>
<li>ord(‘字符’) 可以查询基于Unicode的编码</li>
<li>chr() 参数主要为待查询的编码序号</li>
</ul>
</li>
<li><p>方法：</p>
<ul>
<li>encode() 指定字符编码格式</li>
<li>decode() 指定字符解码格式</li>
</ul>
</li>
</ul>

        </div>

        <blockquote class="post-copyright">
    
    <div class="content">
        
<span class="post-time">
    Last updated: <time datetime="2020-07-17T11:04:58.986Z" itemprop="dateUpdated">2020-07-17 19:04:58</time>
</span><br>


        
        这里可以写作者留言，标签和 hexo 中所有变量及辅助函数等均可调用，示例：<a href="/blog/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" target="_blank" rel="external">https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/</a>
        
    </div>
    
    <footer>
        <a href="https://gitee.com/songbug/blog.git">
            <img src="/blog/img/avatar.jpg" alt="Song Wani">
            Song Wani
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            
	<ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/blog/tags/%E7%BB%8F%E9%AA%8C/" rel="tag">经验</a></li></ul>


            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&title=《Python常用方法及函数》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&title=《Python常用方法及函数》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Python常用方法及函数》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/blog/2020/07/17/%E4%BD%BF%E7%94%A8range()%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%A4%96%E6%98%9F%E4%BA%BA/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">使用range()自动生成外星人</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/blog/2020/07/16/%E5%8D%83%E5%B3%B0%E6%95%99%E7%A8%8B%E7%AC%94%E8%AE%B0/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">千峰教程笔记</h4>
      </a>
    </div>
  
</nav>



    




















</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/blog/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/blog/img/wechat.jpg" data-alipay="/blog/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
            <span>This blog is licensed under a <a rel="license noopener" href="https://creativecommons.org/licenses/by/4.0/" target="_blank">Creative Commons Attribution 4.0 International License</a>.</span>
        </p>
    </div>
    <div class="bottom">
        <p><span>Song Wani &copy; 2019 - 2021</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&title=《Python常用方法及函数》 — SongKaBaKa&pic=https://gitee.com/songbug/blog.git/img/avatar.jpg" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&title=《Python常用方法及函数》 — SongKaBaKa&source=" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《Python常用方法及函数》 — SongKaBaKa&url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/&via=https://gitee.com/songbug/blog.git" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=https://gitee.com/songbug/blog.git/2020/07/17/Python%E5%B8%B8%E7%94%A8%E7%9A%84%E6%96%B9%E6%B3%95%E5%8F%8A%E5%87%BD%E6%95%B0/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/blog/', SHARE: true, REWARD: true };


</script>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/main.min.js"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="//unpkg.com/hexo-theme-material-indigo@latest/js/search.min.js" async></script>






<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
